//
//  ViewController.m
//  ScrollViewDemo
//
//  Created by SmartStrategy on 2019/4/8.
//  Copyright © 2019 SmartStrategy. All rights reserved.
//

#import "ViewController.h"
#import "HomeView.h"
#import "HomeTableView.h"
#import "HomeTableViewCell.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) HomeTableView *tableView;
@property (nonatomic, strong) HomeView *headerView;
@property (nonatomic, strong) HomeTableViewCell *contentCell;

// 页面是否可以滚动
@property (nonatomic, assign) BOOL canScroll;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeScrollStatus) name:@"changeScrollStatus" object:nil];
    // 默认页面可以滚动
    self.canScroll = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass([self class]);
    
    self.tableView.tableHeaderView = self.headerView;
    [self.view addSubview:self.tableView];
    
    
    
}

- (void)changeScrollStatus { //改变主视图的状态
    self.canScroll = YES;
    self.contentCell.cellCanScroll = NO;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [UIScreen mainScreen].bounds.size.height - 40 -44 - 83;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuseIdentifier = @"HomeTableViewCell";
    self.contentCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!self.contentCell) {
        __weak typeof (self)weakSelf = self;
        self.contentCell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        [self.contentCell setPageChangedBlock:^(NSInteger selectedIndex) {
            // 当在切换Page选项卡的时候，再去更新一下内、外部是否能滚动的情况。
            [weakSelf scrollViewDidScroll:tableView];
        }];
        
    }
    return self.contentCell;
}





- (HomeTableView *)tableView {
    if (!_tableView) {
        _tableView = [[HomeTableView alloc] initWithFrame:CGRectMake(0, 84, 414, 896-84-83) style:UITableViewStylePlain];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _tableView.backgroundColor = UIColor.greenColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
    }
    return _tableView;
}


- (HomeView *)headerView {
    if (!_headerView) {
        _headerView = [[HomeView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 500)];
    }
    return _headerView;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat h = self.headerView.bounds.size.height;
    if (scrollView == self.tableView) {
        if (fabs(scrollView.contentOffset.y - h) <= 0.5) {// 滚动距离为 headerView 高度的那一刻
            if (self.canScroll) {
                self.canScroll = NO;
                self.contentCell.cellCanScroll = YES;
            } else {
                self.contentCell.cellCanScroll = YES;
            }
        } else {
            if (!self.canScroll) {//子视图没到顶部
                scrollView.contentOffset = CGPointMake(0, h);
            }
        }
        // 告知视图能否内部vc刷新
        if (scrollView.contentOffset.y == 0) {
            self.contentCell.isRefresh = YES;
        } else {
            self.contentCell.isRefresh = NO;
        }
    }
    
}


@end
